<!-- Horizontal Form -->
 <div class="box box-info">
      <div class="box-header with-border">
        <h3 class="box-title">添加分类</h3>
        <h3 class="msg"></h3>
      </div>
      <!-- /.box-header -->
      <!-- form start -->
      <form class="form-horizontal">
        <div class="box-body">
         
          <div class="form-group">
            <label for="typenameId" class="col-sm-2 control-label">类名</label>
            <div class="col-sm-10">
              <input type="text" name="goodsname" class="form-control" id="typenameId">
            </div>
          </div>

        </div>
        <!-- /.box-body -->
        <div class="box-footer">
          <button type="button" class="btn btn-default btn-cancel">Cancel</button>
          <button type="button" class="btn btn-info pull-right btn-save">Save</button>
        </div>
        <!-- /.box-footer -->
      </form>
    </div>
<script type="text/javascript" src="bower_components/layer/layer.js"></script>
<script type="text/javascript">

 $(document).ready(function(){
	 //页面加载完成以后加载角色信息并初始化页面
     doInitFormData();
	 //事件注册
	 $(".box-footer")
	 .on("click",".btn-cancel",doCancel)
	 .on("click",".btn-save",doSaveOrUpdate);
	 
	  $("#treeLayer")
	  .on("click",".btn-cancel",doHideTree)
	  .on("click",".btn-confirm",doConfirm);
	 
	 $(".form-horizontal")
	 .on("click",".load-sys-dept",doLoadZTreeNodes);
 });

 //确定按钮
 function doConfirm(){
	  //1.获取选中的记录(id,name);
	  var selectedNodes=zTree.getSelectedNodes();
	  var node=selectedNodes[0];
	  //2.将id和name填写或绑定在具体对象上
	  $("#deptId").val(node.name);
	  console.log("node.id="+node.id)
	  $("#deptId").data("deptId",node.id)
 }
 

 function doCancel(){
	$("#mainContentId").removeData("rowData");
	$("#mainContentId").load("type/type_list");
 }
 function doSaveOrUpdate(){
	 var rowData=$("#mainContentId").data("rowData");
	 //1.获取表单数据
	 var params=doGetEditFormData();
	 if(rowData)params.id=rowData.id;
	 //2.发起异步请求
	 var insertUrl="type/doSaveObject";
	 var updateUrl="type/doUpdateObject";
	 var url=rowData?updateUrl:insertUrl;
	 console.log(params);
	 $.post(url,params,function(result){
		 if(result.state==1){
			 alert(result.message);
			 doCancel();
		 }else{
			alert(result.message);
		 }
	 })
 }
 function doGetEditFormData(){
	 //获取用户输入的数据
	 var params={
        typeName:$("#typenameId").val(),
	 }
	 return params;
 }



 function doInitFormData(){
     var data=$("#mainContentId").data("rowData");
     if(!data)return;
	 console.log(data);
	 //初始化用户信息
	 $("#typenameId").val(data.typeName);

	 //初始化用户角色信息
	 // var ids=data.roleIds;
	 // for(var i in ids){
	 //  $("#rolesId input[value='"+ids[i]+"']")
	 //  .prop("checked",true);
	 // }
 }
</script>
    
    
    
    
    
    
    